package spreadsheet;

import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.jdom2.Element;
import org.jdom2.input.DOMBuilder;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

 /**
  * 
  * @author Ahmed Saliem
  * This class is used to read xml file and load it into 2D-array.
  *
  */
public class JDOMXMLReader {
 
   /**
    * this method id used to read the xml file and load it into a 2D-array.
    */
    public static boolean readXmlFile(String path, SpreadSheetCell[][] cells )
	{
		
            org.jdom2.Document jdomDoc;
            try {
                //create JDOM Document from DOM
                jdomDoc = useDOMParser(path);
                org.jdom2.Element root = jdomDoc.getRootElement();
                List<Element> cellListElements = root.getChildren("SpreadSheetCell");           
                int row,col;
                for (Element cellElement : cellListElements) {
                    SpreadSheetCell cell = new SpreadSheetCell();
                    cell.setCellType(cellElement.getChildText("CellType"));
                    cell.setCellValue((cellElement.getChildText("CellValue")));
                    row = Integer.parseInt(cellElement.getAttributeValue("CellRow"));
                    col = Integer.parseInt(cellElement.getAttributeValue("CellCol"));
                    cells[row][col] = cell;

                }

                return true;
            } catch (Exception e) {
                return false;
            }
        
	}
    
    /**
     * Method for reading a file when the dimensions of the output array are unknown
     */
    public static SpreadSheetCell[][] readXmlFile( String path ) throws ParserConfigurationException, SAXException, IOException {
        //create JDOM Document from DOM
        if( path != null && path.length() > 0 && new File( path ).exists() ) {
            org.jdom2.Document jdomDoc = useDOMParser(path);
            org.jdom2.Element root = jdomDoc.getRootElement();
            List<Element> cellListElements = root.getChildren("SpreadSheetCell");
            int iRowCount = 0;
            int iColCount = 0;
            for (Element cellElement : cellListElements) {//first detect the size of the array
                int row = Integer.parseInt(cellElement.getAttributeValue("CellRow"));
                int col = Integer.parseInt(cellElement.getAttributeValue("CellCol"));
                iRowCount = Math.max( row, iRowCount );
                iColCount = Math.max( col, iColCount );
            }
            SpreadSheetCell[][] cells = new SpreadSheetCell[iRowCount+1][iColCount+1];
            for (Element cellElement : cellListElements) {
                SpreadSheetCell cell = new SpreadSheetCell();
                cell.setCellType(cellElement.getChildText("CellType"));
                cell.setCellValue((cellElement.getChildText("CellValue")));
                int row = Integer.parseInt(cellElement.getAttributeValue("CellRow"));
                int col = Integer.parseInt(cellElement.getAttributeValue("CellCol"));
                cells[row][col] = cell;
            }
            return cells;
        }
        return null;
    }
    
    //Get JDOM document from DOM Parser
    private static org.jdom2.Document useDOMParser(String fileName)
            throws ParserConfigurationException, SAXException, IOException {
        //creating DOM Document
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder;
        dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(new File(fileName));
        DOMBuilder domBuilder = new DOMBuilder();
        return domBuilder.build(doc);
 
    }
}
    